package com.code2roc.miniflow.base.util;

import com.code2roc.miniflow.base.annotation.MiniFlowColumn;
import com.code2roc.miniflow.base.annotation.MiniFlowTable;
import com.code2roc.miniflow.base.model.MiniFlowSQLInfo;

import java.lang.reflect.Field;
import java.util.HashMap;

public class MiniFlowSQLGenerateUtil {

    public static MiniFlowSQLInfo generateInsertInfo(Object model) {
        MiniFlowSQLInfo sqlInfo = new MiniFlowSQLInfo();
        String sqlText = "";
        HashMap<String, Object> paramMap = new HashMap<>();
        try {
            MiniFlowTable table = model.getClass().getAnnotation(MiniFlowTable.class);
            String tableName = table.value();
            for (Field field : MiniFlowReflectUtil.getAllFields(model.getClass())) {
                field.setAccessible(true);
                if (field.isAnnotationPresent(MiniFlowColumn.class)) {
                    MiniFlowColumn column = field.getAnnotation(MiniFlowColumn.class);
                    String columnName = column.value();
                    Object value = field.get(model);
                    paramMap.put(columnName, value);
                }
            }
            sqlText += "insert into " + tableName + " ( ";
            for (String name : paramMap.keySet()) {
                sqlText += name + ",";
            }
            sqlText = sqlText.substring(0, sqlText.length() - 1);
            sqlText += " ) values ( ";
            for (String name : paramMap.keySet()) {
                sqlText += "#{" + name + "},";
            }
            sqlText = sqlText.substring(0, sqlText.length() - 1);
            sqlText += " )";
        } catch (Exception e) {
            e.printStackTrace();
        }
        sqlInfo.setSqlText(sqlText);
        sqlInfo.setParamMap(paramMap);
        return sqlInfo;
    }

    public static MiniFlowSQLInfo generateUpdateInfo(Object model) {
        MiniFlowSQLInfo sqlInfo = new MiniFlowSQLInfo();
        String sqlText = "";
        HashMap<String, Object> paramMap = new HashMap<>();
        try {
            MiniFlowTable table = model.getClass().getAnnotation(MiniFlowTable.class);
            String tableName = table.value();
            for (Field field : MiniFlowReflectUtil.getAllFields(model.getClass())) {
                field.setAccessible(true);
                if (field.isAnnotationPresent(MiniFlowColumn.class)) {
                    MiniFlowColumn column = field.getAnnotation(MiniFlowColumn.class);
                    String columnName = column.value();
                    Object value = field.get(model);
                    paramMap.put(columnName, value);
                }
            }
            sqlText += "update  " + tableName + " set ";
            for (String name : paramMap.keySet()) {
                sqlText += name + "=#{" + name + "},";
            }
            sqlText = sqlText.substring(0, sqlText.length() - 1);
            sqlText += " where rowguid=#{rowguid}";
        } catch (Exception e) {
            e.printStackTrace();
        }
        sqlInfo.setSqlText(sqlText);
        sqlInfo.setParamMap(paramMap);
        return sqlInfo;
    }
}
